/**
 * 
 */
package com.cqan.account.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.cqan.entity.pk.LongEntity;
import com.cqan.util.ConvertUtils;

/**
 * @Description TODO
 * @author slaton.wu@gmail.com
 * @date 2012-6-29下午6:48:21
 *
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "TBL_ROLE")
//默认的缓存策略.
@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends LongEntity{

	private List<Authority> authorities;
	
	private Date createTime;
	
	private boolean enabled;
	
	private String name;
	
	private List<User> users;

	
	// 多对多定义
		@ManyToMany
		// 中间表定义,表名采用默认命名规则
		@JoinTable(name = "TBL_ROLE_AUTH", joinColumns = { @JoinColumn(name = "RID") }, inverseJoinColumns = { @JoinColumn(name = "AID") })
		// Fecth策略定义
		@Fetch(FetchMode.SUBSELECT)
		// 集合按id排序.
		@OrderBy("id")
		// 集合中对象id的缓存.
		@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Authority> getAuthorities() {
		return authorities;
	}

	@Column(name="CREATE_TIME")
	public Date getCreateTime() {
		return createTime;
	}

	@Column(name="NAME")
	public String getName() {
		return name;
	}

	@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "roles",fetch = FetchType.LAZY)
	public List<User> getUsers() {
		return users;
	}

	@Column(name="ENABLED")
	public boolean isEnabled() {
		return enabled;
	}

	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getAuthIds() {
		return ConvertUtils.convertElementPropertyToList(authorities, "id");
	}
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
